//
//  SearcherViewController.m
//  YouMin_iOS
//
//  Created by lanou3g on 15/12/11.
//  Copyright © 2015年 wey. All rights reserved.
//

#import "SearcherViewController.h"
#import "UIButton+url.h"
#import "NearByViewController.h"
static NSString *const baseUrl = @"http://api.breadtrip.com/destination";

@interface SearcherViewController ()<UISearchBarDelegate>

@property (weak, nonatomic) IBOutlet UILabel *firstlabel;
///  搜索Bar
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UIView *firstVIew;
@property (weak, nonatomic) IBOutlet UILabel *secondLabel;
@property (weak, nonatomic) IBOutlet UIView *secondView;
@property (nonatomic, assign) BOOL isCancel;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;

@end

@implementation SearcherViewController
#pragma mark --- array 不固定不一定可能会炸
- (void)viewDidLoad {
    [super viewDidLoad];
    self.firstlabel.text = self.items.firstObject[@"title"];
    self.secondLabel.text = self.items[1][@"title"];
    self.searchBar.delegate = self;
    NSArray *firstArr = self.items[0][@"elements"];
    NSArray *secondArr = self.items[1][@"elements"];
    CGFloat buttonWidth = self.firstVIew.frame.size.width/4;
    CGFloat buttonHeight = self.firstVIew.frame.size.height/5;
    self.isCancel = YES;
    
    NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:firstArr.count];
    NSMutableArray *firstUrls = [NSMutableArray arrayWithCapacity:firstArr.count];
    [firstArr enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [arrM addObject:obj[@"name"]];
        [firstUrls addObject:obj[@"url"]];
    }];
    
    int firstcount = 0;
    for (int a=0; a<firstArr.count/3 - 1; a++) {
        
        for (int b =0; b<3; b++) {
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.layer.cornerRadius = 10;
            button.layer.borderColor = [UIColor darkGrayColor].CGColor;
            button.layer.borderWidth = 1.0;
            
            button.frame = CGRectMake(a * (buttonWidth+2), b*(buttonHeight+2), buttonWidth, buttonHeight);
            [button setTitle:arrM[firstcount] forState:UIControlStateNormal];
            [button setTintColor:[UIColor darkGrayColor]];
            [self.firstVIew addSubview:button];
            if (firstcount <arrM.count-1) {
                firstcount++;
            }
            button.url = firstUrls[firstcount];
            [button addTarget:self action:@selector(firstAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.firstVIew addSubview:button];
        }
        
    }
    
    NSMutableArray *arrM2= [NSMutableArray arrayWithCapacity:secondArr.count];
    NSMutableArray *secondUrls = [NSMutableArray arrayWithCapacity:secondArr.count];
    [secondArr enumerateObjectsUsingBlock:^(NSDictionary * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [arrM2 addObject:obj[@"name"]];
        [secondUrls addObject:obj[@"url"]];
    }];
    
    buttonWidth = self.firstVIew.frame.size.width/5;
    buttonHeight = self.firstVIew.frame.size.height/5;
    
    int senondCount = 0;
    for (int a= 0; a<5; a++) {
        for (int b =0; b<secondArr.count/5; b++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.layer.cornerRadius = 10;
            button.layer.borderColor = [UIColor darkGrayColor].CGColor;
            button.layer.borderWidth = 1.0;
            
            button.frame = CGRectMake(a * (buttonWidth+2), b*(buttonHeight+5), buttonWidth, buttonHeight);
            [button setTitle:arrM2[senondCount] forState:UIControlStateNormal];
            [button setTintColor:[UIColor darkGrayColor]];
            
            [self.firstVIew addSubview:button];
            if (senondCount <arrM2.count-1) {
                senondCount++;
            }
            button.url = secondUrls[senondCount];
            [button addTarget:self action:@selector(firstAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.secondView addSubview:button];
        }
    }
}

- (void)firstAction:(UIButton *)sender{
    
    NSString *url = [sender.url stringByReplacingOccurrencesOfString:@"scenic" withString:@"place"];
    url = [baseUrl stringByAppendingString:url];
    
}


- (IBAction)butonBackAction:(id)sender {
    UIButton *button = sender;
    if ([[button titleForState:UIControlStateNormal] isEqualToString:@"搜索"]) {
        [self searchBarSearchButtonClicked:self.searchBar];
    }else {
        [self.cancelButton setTitle:@"搜索" forState:UIControlStateNormal];
        [self.searchBar resignFirstResponder];
    }
    [self.searchBar resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    
    NSString *baseUrl2 = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/search/?key=%@&start=0&count=20&data_type=",searchBar.text];
    [[NetWorkHelper shareInstance] GET:baseUrl2 parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSLog(@"%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@,%s",error,__func__);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.searchBar resignFirstResponder];
}
@end
